<?php
namespace Platform\Domain;

use Platform\Model\Articles as Model;
use PhalApi\Exception\BadRequestException;

class Articles {

    public function getList($where,$page,$limit,$order) {
        $model = new Model();
        $list = array();
        $outData = array();
        $totalPage = 0;
        $where['delete_flag'] = 'N';
        $count = $model->count($where,'id');
        //总分页数
        if( $count>0 ){
            $totalPage = ceil($count/$limit);
        }
        //如果在分页范围内就获取列表接口
        if($totalPage>=$page){
            $list = $model->getList($where,array(),'*',$order,$page,$limit);
        }
        $outData['items'] = $list;
        $outData['total'] = $count;
        return $outData;
    }

    public function add($data) {
        $model = new Model();
        $id = $model->insert($data);
        return $id;
    }

    public function detail($id) {
        $model = new Model();
        $info = $model->getData(array('delete_flag'=>'N','id'=>$id));
        if( empty( $info ) ){
            throw new BadRequestException('数据不存在或已被删除！');
        }
        $model->update($id,array('read_num'=>$info['read_num']+1));
        return $info;
    }

    public function edit($id,$data) {
        $model = new Model();
        $data['update_time'] = date('Y-m-d H:i:s',time());
        return $model->update($id,$data);
    }
    
    public function delete($id) {
        $model = new Model();
        return $model->update($id,array('delete_flag'=>'Y'));
    }

}
